#ifndef GAME_H
#define GAME_H

#include "glstuff.h"
#include "cube.h"
#include "math_stuff.h"
#include "create_cubes.h"
#include "cubes_find_target.h"
#include "cubes_rotate.h"
#include "cubes_move.h"
#include "game_test.h"
#include "cube_image.h"
#include "draw.h"


/*
ColorRGB cols[12]= {//255 50 100 -> 255 0 255
{255, 50, 125}, {255, 50, 145}, {255, 50, 160}, 
{255, 50, 175}, {255, 50, 185}, {255, 50, 195}, 
{255, 50, 205}, {255, 50, 215}, {255, 50, 225}, 
{255, 50, 235}, {255, 50, 245}, {255, 50, 255} 
};
*/
ColorRGB cols[12]= {//255 50 100 -> 255 0 100
{100, 50, 255}, {105, 50, 230}, {120, 50, 217}, 
{135, 50, 204}, {150, 50, 191}, {165, 50, 178}, 
{180, 50, 165}, {195, 50, 152}, {210, 50, 139}, 
{225, 50, 126}, {240, 50, 113}, {255, 50, 100} 
};

ColorRGB cols2[12]= {
{90, 90, 0}, {105, 105, 0}, {120, 120, 0}, 
{135, 135, 0}, {150, 150, 0}, {165, 165, 0}, 
{180, 180, 0}, {195, 195, 0}, {210, 210, 0}, 
{225, 225, 0}, {240, 240, 0}, {255, 255, 0} 
};

int game_init()
{	   
    int i;
    SDL_Surface* img_orig;
    SDL_Surface* img;
    float ambient[] = {1, 1, 1, 1}; 
    srand(SDL_GetTicks());
    load_image(&field_text, "./data/field.tga", NotAlpha);
    img_orig = load_image_to_surface("./data/cube.tga",
        NotAlpha);
    load_table(); 
    make_marks();
    make_color_range(Levels_Count, colors);
    make_slice_color_range(Levels_Count-1, colors1, 
        51, 255);
    make_slice_color_range(Levels_Count-1, colors2, 
        101, 255);
        
    img = copy_surface(img_orig);
    make_target_image(img);
    SDL_FreeSurface(img);
    img = copy_surface(img_orig);
    make_wait_image(img);
    SDL_FreeSurface(img);    
        
    for(i = 0; i < Levels_Count; ++i)
    {
        img = copy_surface(img_orig);
        load_cube_image(&cube_texts[i], i+1, img);
        SDL_FreeSurface(img);
    }  
    
    
    SDL_FreeSurface(img_orig);
    create_cubes(); 
    //create_less_cubes(); 
    //create_test_cubes();
	glClearColor(0.1, 0.4, 0.3, 0);
	glColor3f(1.0f, 0.0f, 0.0f);   
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);               
	glViewport(0, 0, W, H);
	glMatrixMode(GL_PROJECTION); 
    glLoadIdentity();
    //l r b t 
    glOrtho(-W/2, W/2, -H/2, H/2, -1000, 1000);
    glMatrixMode(GL_MODELVIEW);
	return 1;   
}

int draw_game()
{
    int x, y;
    //float ambient[] = {0.15, 0.15, 0.15, 1};
    float ambient[] = {0.1, 0.1, 0.1, 1};
    //float diff[] = {1, 1, 1, 1};
    
	glClear(GL_COLOR_BUFFER_BIT);
	
	glLoadIdentity();
	// eye x y z, center x y z, up x y z
	gluLookAt(0, 0, 1,  0, 0, 0,  0, 1, 0);
	
	glEnable(GL_LIGHTING); 
	glMaterialfv(GL_FRONT, GL_AMBIENT, ambient); 
	//glMaterialfv(GL_FRONT, GL_DIFFUSE, diff);
	glDisable(GL_BLEND);
	glEnable(GL_TEXTURE_2D);
	
	glBindTexture(GL_TEXTURE_2D, field_text);
	for(x = -W/2; x < W/2; x+=64) {
	   for(y = H/2; y > -H/2; y-=64) {
	       draw_square(x, y, 64);
	   }
	}

	draw_cubes(Draw_Passive);
	draw_cubes(Draw_Active);
	
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_LIGHTING);
	SDL_GL_SwapBuffers();
	return 1;
}

int game_main()
{
    if(cubes_select_target() != 2) {
        cubes_rotate();
        cubes_move();
    } else {
        game_restart();
    }
    draw_game();
    
	gl_print_error();
	wait(Wait_Normal);
	print_fps();
	return 1;
}

#endif
